# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@rules_foreign_cc//foreign_cc:make.bzl", "make")

package(default_visibility = ["//src:__subpackages__"])

filegroup(
    name = "preset_queries",
    srcs = glob(
        [
            "**/*.pxl",
            "**/*.json",
            "**/*.yaml",
        ],
        exclude = ["bundle-oss.json"],
    ),
    visibility = ["//src:__subpackages__"],
)

genrule(
    name = "script_bundle",
    srcs = [
        ":Makefile",
        ":preset_queries",
    ],
    outs = ["bundle-oss.json"],
    cmd = """
        export PATH_PREFIX=$$(dirname $(location //src/pxl_scripts:Makefile))/;
        EXECUTABLES=../../$(location //src/pixie_cli:px) make -C $$PATH_PREFIX bundle-oss.json;
        cp bundle-oss.json $(@D)/bundle-oss.json
    """,
    tools = [
        "//src/pixie_cli:px",
    ],
    visibility = ["//src:__subpackages__"],
)

sh_test(
    name = "script_bundle_test",
    srcs = ["test_script_bundle.sh"],
    args = [
        "$(location :script_bundle)",
        "$(location @com_github_mikefarah_yq_v4//:v4)",
    ],
    data = [
        ":script_bundle",
        "@com_github_mikefarah_yq_v4//:v4",
    ],
)
